<template>
    <div class="box"
         :class="{'global_love':$store.state.nowTime==='moon'}">
        <div class="box_content"
             :class="{'global_word_back':$store.state.nowTime==='moon'}">
           <div :class="{'global_font':$store.state.nowTime==='moon'}" class="auto">
              QQ: <input type="text" placeholder="必填" class="qq" v-model="qq" maxlength="20">
           </div>
            <textarea contenteditable="true" class="content"  ref="edit" @focus="ess" @blur="ess_b" ></textarea>
        </div>

        <div class="function">
            <div class="choose">
                <span @click="exp_s" ><img src="/img/common/biaoqing.png" alt="" title="表情"  class="send"></span>
                <button @click="send">发送评论</button>
            </div>
            <div class="expression" v-show="expression" >
                <ul >
                    <li @click="expression_li($event)" v-for="item in expression_e" v-html="item"></li>
                </ul>
            </div>
        </div>
    </div>
</template>



<script >
import api from "@/api";

export default {
    name: "WordPad",
    data(){
        return{
            expression:false,
            content:"勿喷，谢谢配合。",
            loveComments:{
                url:"",
                content:"",
            },
            bulidIndex:0,//光标位置
            qq:"",
            expression_e:['😀', '😄', '😁', '😅', '🤣', '🙂', '😉', '😊', '🥰', '😍', '😘', '🤑',
                         '😚', '😑', '😏', '😪', '😎', '😾', '😭', '😓', '😖', '👋', '🤬', '😷',
                         '🤒', '🤕', '🤢', '🤮', '🤧', '😇', '🤠', '🤡', '🤥', '🤫', '🤭', '🧐',
                         '🤓', '😈', '👿', '👹', '👺', '💀', '👻', '👽', '🤖', '💣', '💩', '😺',
                         '😸', '😹', '😻', '😼', '😽', '🙀', '😿', ]
        }
    },
    mounted() {

    },
    watch:{

    },
    methods: {
        expression_li(e){
            let text =  this.$refs.edit.value;
            this.$refs.edit.value= text.slice(0, this.bulidIndex) + e.target.innerText + text.slice(this.bulidIndex);
        },
        ess_b(e){
            this.bulidIndex = e.target.selectionStart;
        },
        ess(e) {

            if(this.$refs.edit.value === "")
                this.$refs.edit.value = "";
        },
        exp_s() {
            this.expression = !this.expression;
        },
        send() {
            if(this.$refs.edit.value === "" || this.$refs.edit.value.length>1000){
                alert("回复内容不能为空,或长度过长！")
                return;
            }
            if(this.$refs.edit.value.length>1000){
                alert("最多可输入200个字！")
                return;
            }
            if(!(/^[1-9]\d{4,9}$/.test(String(this.qq)))){
                alert("无效QQ！")
                return;
            }
            this.loveComments.content = this.$refs.edit.value;
            this.loveComments.url = this.qq;
            this.$emit("handleCommentAdd",this.loveComments)
            api.loveInsert(this.loveComments).then((res)=>{

            })
            this.$refs.edit.value = "";
               this.expression = false;
               this.qq = "";
        },
    }
}
</script>



<style scoped>
.box{
    margin-top: 20px;
    box-shadow: 0 1px 1px 0 rgba(0, 0, 0, 0.2), 0 2px 5px 0 rgba(0, 0, 0, 0.19);
    background: #fff;
    color: #8f8c8c;
}
.content{
    width: 100%;
    min-height: 100px;
    max-height: 300px;
    color: #838080;
    overflow: initial;
    padding: 10px;
    outline: none;
    resize: vertical;
    border: 1px solid #8f8c8c !important;
}

.box_content{
    padding: 10px;background: white;
    border-top-right-radius: 5px;
    border-top-left-radius: 5px;
    color: #fff;
}
.function{
    width: 100%;
    min-height: 50px;
    padding: 0 20px;

}
.choose{
    display: flex;
    justify-content: space-between;
    align-items: center;

}
button{
    padding: 10px 13px;
    background: #3586f1;
    color: white;
    border:0;
}
button:hover{
    opacity: .9;
}
button:hover,img:hover{
    cursor: pointer;
}
.expression{
    width: 100%;
    margin-top: 10px;
    padding: 5px;
    animation: box .5s ease-in-out 0s 1 forwards;
    border-radius: 4px;
}
.expression ul{
    display: flex;
    flex-flow:row wrap;
    justify-content: space-around;
    align-items: center;

}
.expression ul li{
    list-style: none;
    font-size: 20px;
}
.expression ul li:last-child{
    margin-right: auto !important;
}
.expression ul li:hover{
    cursor: pointer;
}
@keyframes box {
    0%{
        transform: scale(0);
    }
    70%{
        transform: scale(1.1);
    }
    100%{
        transform: scale(1);
    }
}
.qq{
    height: 30px;
    outline: none;
    color: #8f8c8c;
    margin-bottom: 5px;
    width: 200px;
    border: 0;
    border-bottom: 1px solid;
    background: transparent !important;
}

.auto{
    color: #8f8c8c;
}
.send{
    width:30px
}
</style>